//
//  FeedbackVC.swift
//  甜言蜜语
//
//  Created by yingli_yanfa_liuxingyu on 16/8/22.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit
import SVProgressHUD
class FeedbackVC: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate,UITextViewDelegate {
    @IBOutlet weak var message: UITextView!
    @IBOutlet weak var contact: UITextField!
    @IBOutlet weak var ivBtn: UIButton!
    @IBOutlet weak var complet: UIButton!
    @IBOutlet weak var lblPlace: UILabel!
    @IBOutlet weak var hiddenLbl: UILabel!
    var actionSheet: UIActionSheet? = nil
    var imagePicker: UIImagePickerController? = nil
    var msg: regiestInfo?
    var img: UIImage?
    override func viewDidLoad() {
        super.viewDidLoad()
        publicMethod.setHUDStyle()
        self.complet.layer.masksToBounds = true
        self.complet.layer.cornerRadius = 20
        self.actionSheet?.delegate = self
        self.message.layer.borderWidth = 1
        self.message.delegate = self
        self.contact.tintColor = UIColor.blackColor()
        if (actionSheet == nil) {
            self.actionSheet = UIActionSheet(title: "选择照片",delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: nil, otherButtonTitles: "拍照", "从手机相册选择")
        }

        
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.barTintColor = NAVIGATIONBAR_RED_COLOR
        
    }
    override func viewWillDisappear(animated: Bool) {
        SVProgressHUD.dismiss()
        super.viewWillDisappear(animated)
    }
    //MARK:target
    @IBAction func ivBtnClicked(sender:UIButton){
        self.actionSheet!.showInView(self.view!)
        
    }
    @IBAction func submitBtnClicked(sender: UIButton){
        if self.message.text == ""{
            SVProgressHUD.showErrorWithStatus("请填写反馈信息")
           
            return
        }
        if self.contact.text == ""{
            SVProgressHUD.showErrorWithStatus("请填写联系方式")
           
            return
            
        }else{
            
            let param = ["contact":self.contact.text!, "message":self.message.text!]
            
            
            
            DataTool.feedBack(param,img:self.img, completionHandler: { (newModel) in
                guard newModel != nil else {
                    return
                }
                self.msg = newModel
                SVProgressHUD.showSuccessWithStatus(self.msg?.msg)
                
            })

            
        }
        
    }
    
    
    //MARK:UITextviewDelegate
    
    func textViewDidChange(textView: UITextView) {
        if message.text.characters.count > 0 {
            self.lblPlace.alpha = 0
        }
        else {
            self.lblPlace.alpha = 1
        }
    }
    
    //MARK:actionSheetDelegate
    func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
        if buttonIndex == 1 {
            //拍照
            var sourceType = UIImagePickerControllerSourceType.Camera
            if !UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
                
                sourceType = UIImagePickerControllerSourceType.PhotoLibrary
            }
            let picker = UIImagePickerController()
            picker.delegate = self
            picker.allowsEditing = true//设置可编辑
            picker.sourceType = sourceType
            self.presentViewController(picker, animated: true, completion: nil)//进入照相界面
            
        }else if buttonIndex == 2{
            //相册
            let pickerImage = UIImagePickerController()
            if !UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary){
                pickerImage.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
                pickerImage.mediaTypes = UIImagePickerController.availableMediaTypesForSourceType(pickerImage.sourceType)!
            }
            
            pickerImage.delegate = self
            
            pickerImage.allowsEditing = true
            
            self.presentViewController(pickerImage, animated: true, completion: nil)
        }
        
    }
    //MARK:UIImagePickerControllerDelegate
    func imagePickerController(picker: UIImagePickerController,didFinishPickingMediaWithInfo info: [String : AnyObject]){
        
        var img: UIImage!
        self.hiddenLbl.hidden = true
        img = info[UIImagePickerControllerEditedImage] as! UIImage
        self.img = img
        self.ivBtn.setImage(img, forState: UIControlState.Normal)
        picker.dismissViewControllerAnimated(true, completion: nil)
        
    }
    func imagePickerControllerDidCancel(picker: UIImagePickerController){
        
        print("cancel--------->>")
        
        picker.dismissViewControllerAnimated(true, completion: nil)
        
    }
}

